package com.alvinlkk.basic.chapter21;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

class Meal {
    private final int orderNum;
    
    Meal(int orderNum) {
        this.orderNum = orderNum;
    }
    
    @Override
    public String toString() {
        return "Meal = " + orderNum;
    }
}

class WaitPerson implements Runnable {
    
    private Restaurant restaurant;
    
    public WaitPerson(Restaurant restaurant) {
        this.restaurant = restaurant;
    }

    @Override
    public void run() {
        try {
            while(!Thread.interrupted()) {
                synchronized (this) {
                    while(restaurant.meal == null) {
                        wait();
                    }
                    
                    System.out.println("WaitPerson got " + restaurant.meal);
                    
                    synchronized (restaurant.chef) {
                        restaurant.meal = null;
                        restaurant.chef.notifyAll();
                    }
                    
                    synchronized (restaurant.boy) {
                        restaurant.boy.notifyAll();
                    }
                } 
            }
        } catch (InterruptedException e) {
            System.out.println("WaitingPerson interrupted....");
        }
        
    }
    
}

class Chef implements Runnable {

    private Restaurant restaurant;
    
    private int count = 0;
    
    public Chef(Restaurant restaurant) {
        this.restaurant = restaurant;
    }
    
    @Override
    public void run() {
        try {
            while (!Thread.interrupted()) {
                synchronized (this) {
                    while (restaurant.meal != null) {
                        wait();
                    }
                }
                
                if(++count == 10) {
                    System.out.println("out of food closing");
                    restaurant.exec.shutdownNow();
                    return ;
                }
                System.out.println("ORder up !");
                
                synchronized (restaurant.waitPerson) {
                    restaurant.meal = new Meal(count);
                    restaurant.waitPerson.notifyAll();
                }
                TimeUnit.MILLISECONDS.sleep(100);
            }
            
        } catch (InterruptedException e) {
            System.out.println("Chef interrupted");
        }
    }
    
}

//在上菜之后，waitPerson通知BusyBoy清理
class BusyBoy implements Runnable {

    @Override
    public void run() {
        
        try {
            while (!Thread.interrupted()) {
                synchronized (this) {
                    wait();
                }
                System.out.println("Busy Boy clean---");
            }
            
            
        } catch (Exception e) {
            System.out.println("BusyBoy interrupted");
        }
        
    }
    
}

public class Restaurant {
    
    Meal meal;
    
    WaitPerson waitPerson = new WaitPerson(this);
    
    Chef chef = new Chef(this);
    
    BusyBoy boy = new BusyBoy();
    
    ExecutorService exec = Executors.newCachedThreadPool();
    
    
    
    public Restaurant() {
        exec.execute(waitPerson);
        exec.execute(chef);
        exec.execute(boy);
    }
    
    public static void main(String[] args) {
        new Restaurant();
    }
}
